#Pango is a library for rendering internationalized texts

import pango
import os
import gtk
import gtk.glade
import gtkmozembed
import XMLtoMindmap
import MindmaptoXML
import DrawDiagram

class gtktest:
	def __init__(self, runaslib=True):
		# Load Glade XML
		self.xml = gtk.glade.XML("project1.glade")
		
		# Get Window
		self.w = self.xml.get_widget('Infer')
		self.w.connect("delete_event", gtk.main_quit)
		
		# get widgets for popups
		self.opw = self.xml.get_widget('Open')
		self.data = self.xml.get_widget('entry3')
		self.button_ok = self.xml.get_widget('button6')
		self.button_ok.connect('clicked', self.load_file)
		
		# get left pane and add gecko there
		self.middle_pane = self.xml.get_widget('scrolledwindow2')
		self.port = self.middle_pane.get_child()

		self.moz = gtkmozembed.MozEmbed()
		self.port.add(self.moz)
		self.moz.load_url('file:///home/melina/Documents/misc/olpc/project/code/greece-india/star.png')
		self.moz.show()
		
		#get right pane and add mozilla there
		self.moz2 = gtkmozembed.MozEmbed()
		self.right_pane = self.xml.get_widget('scrolledwindow1')
		self.port_right = self.right_pane.get_child()
		self.port_right.add(self.moz2)
		self.moz2.load_url('http://laptop.org')
		self.moz2.show()

		# Get Windows child
		self.child = self.w.get_child()

		# get the open button
		self.button = self.xml.get_widget('button1')
		self.button.connect('clicked', self.open_listen)

		#get the save button
		self.button_save = self.xml.get_widget('button2')
		self.button_save.connect('clicked', self.save_listen)

		self.button_check = self.xml.get_widget('button3')
		self.button_check.connect('clicked',self.check_listen)

		#get input for drawing mind map
		self.st_node = self.xml.get_widget('entry1')
		self.end_node = self.xml.get_widget('entry2')

		#get ADD/DEL buttons
		self.add_button = self.xml.get_widget('button5')
		self.add_button.connect('clicked',self.add_listen)
	
		self.del_button = self.xml.get_widget('button4')
		self.del_button.connect('clicked',self.del_listen)
		
		self.nodes = []
		self.edges = []

		if not runaslib:
			self.w.show_all()
			self.opw.hide()
			gtk.main()

	def save_listen(self, *args):
		self.saving = self.input + "_solution.xml" 
		#mindmap to xml
		st_xml = MindmaptoXML.XMLmindmap(self.input, self.nodes, self.edges, self.saving)
		st_xml.create_xmlmindmap()
		st_xml.store_xmlmindmap()
		#save
		print self.saving
		return

	#listener when open button is clicked
	def open_listen(self,*args):
		os.system ("rm  *.dot")
		#popup the selection window here and get data
		self.opw.show_all()
		return


	#load file given as input
	def load_file(self,*args):
		self.edges = []
		self.opw.hide_all() 
		self.input = self.data.get_text()
		print self.input
		mindmap = self.input+".xml"
		
		mind = XMLtoMindmap.Mindmap(mindmap)
		self.nodes=[]
		self.nodes = mind.get_mindmap_nodes()
		edges = mind.get_mindmap_edges()
		
		diagram = DrawDiagram.Diagram(self.input)
		#generate diagrams without the edges
		diagram.GenDiagram(self.edges, self.nodes, False)
		diagram.WriteDiagram()
		fil = self.input+".svg"
		diagram.Diagram2Svg(fil)
		
		fil = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+self.input+".svg"
		self.moz.load_url(fil)
		self.moz.show()

		url = "http://en.wikipedia.org"
		self.moz2.load_url(url)
		self.moz2.show()
		return

	def check_listen(self,*args):
		print "print solution up to now"
		#create new mindmap
                diagram = DrawDiagram.Diagram()
                diagram.GenDiagram(self.edges, self.nodes, True)
                diagram.WriteDiagram()
                fil = self.input+"_solution"+".svg"
                diagram.Diagram2Svg(fil)

                #render on screen
                fil2 = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+fil
                self.moz.load_url(fil2)
                self.moz.show()

		#get initial graph
		init = XMLtoMindmap.Mindmap(self.input+".xml")
		init_nodes = init.get_mindmap_nodes()
		init_edges = init.get_mindmap_edges()
		#compare with current state
		for edge in self.edges:
			if edge in init_edges:
				edge.append('green')
				print "correct: draw in green"
			else:	
				edge.append('red')
				print "wrong: draw in red"
		
		#render color
                diagram = DrawDiagram.Diagram()
                diagram.GenColoredDiagram(self.edges, self.nodes, True)
                diagram.WriteDiagram()
                fil = self.input+"_solution"+".svg"
                diagram.Diagram2Svg(fil)

                #render on screen
                fil2 = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+fil
                self.moz.load_url(fil2)
                self.moz.show()
		return


	def add_listen(self,*args):
		start = self.st_node.get_text()
		end = self.end_node.get_text()
		print "adding " +start + " " + end

		edge_added = [start, end]
		self.edges.append(edge_added)
		#create new mindmap
		diagram = DrawDiagram.Diagram()
                diagram.GenDiagram(self.edges, self.nodes, True)
                diagram.WriteDiagram()
                fil = self.input+"_solution"+".svg"
                diagram.Diagram2Svg(fil)

		#render on screen
		fil2 = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+fil
		self.moz.load_url(fil2)
		self.moz.show()

		self.st_node.set_text("")
		self.end_node.set_text("")
		return

	def del_listen(self,*args):
		start = self.st_node.get_text()
		end = self.end_node.get_text()
		print "deleting "+start+" "+end

		node_delete = [start, end]
		if node_delete in self.edges:
			self.edges.remove(node_delete)

		#create new mindmap
                diagram = DrawDiagram.Diagram()
                diagram.GenDiagram(self.edges, self.nodes, True)
                diagram.WriteDiagram()
                fil = self.input+"_solution"+".svg"
                diagram.Diagram2Svg(fil)

                #render on screen
                fil2 = "file:///home/melina/Documents/misc/olpc/project/code/greece-india/"+fil
                self.moz.load_url(fil2)
                self.moz.show()

		self.st_node.set_text("")
		self.end_node.set_text("")
		return

if __name__ == '__main__':
	gtktest(False)

